<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>SVN自动化部署</title>
    <link rel="stylesheet" type="text/css" href="svn_auto_deploy/static/font.css" />
    <style>
body{margin:0;padding:0;color:#444;font-family:-apple-system,BlinkMacSystemFont, "Segoe UI ",Roboto, "Helvetica Neue ",Arial,sans-serif;}
.svn_conter{max-height:500px;color:#444;}
.svn_left_menu{display:inline-block;width:155px;height:400px;background:#f0f0f1;vertical-align:top;}
.svn_left_menu li{padding-left:20px;height:60px;line-height:60px;cursor:pointer;list-style:none;margin:0;}
.svn_left_menu li.active{background:#fff;}
.svn_left_menu .icon{display:inline-block;width:auto;}
.svn_left_menu .icon i{color:#666;font-size:17px;}
.svn_left_menu span{margin-left:8px;color:#666;font-size:15px;}
.svn_rigth_body{position:absolute;top:0;right:0;bottom:0;left:155px;max-height:500px;padding:20px;overflow-y:auto;}
.svn_rigth_body .item_con{display:none;}
.svn_rigth_body .item_con.active{display:block;}
/* 导航标签样式 */
.svn_nav_group{height:36px;line-height:36px;border-bottom:1px solid #ececec;font-size:0;}
.svn_nav_group span{display:inline-block;height:36px;padding:0 25px;font-size:13px;cursor:pointer;color:#444;border:1px solid #fff;border-bottom:none;position:relative;top:-1px;}
.svn_nav_group span.active{border:1px solid #ececec;line-height:34px;color:#444;border-bottom-color:#fff;border-top-left-radius:2px;border-top-right-radius:2px;}
.svn_nav_content{display:block;position:relative;}
.svn_nav_content .svn_nav_item{display:none;padding-top:15px;}
.svn_nav_content .svn_nav_item.active{display:block;}
/* 状态卡片样式 */
.svn_status_grid{display:flex;flex-wrap:wrap;margin:0 -10px;margin-bottom:20px;}
.svn_status_card{flex:1;min-width:300px;background:#fff;border:1px solid #ececec;border-radius:3px;margin:0 10px 15px;padding:15px;}
.svn_status_title{font-weight:500;margin-bottom:10px;color:#444;font-size:14px;}
.svn_status_value{font-size:16px;font-weight:600;}
.svn_status_running{color:#5cb85c;}
.svn_status_stopped{color:#d9534f;}
/* 表单样式 */
.svn_form_group{margin-bottom:15px;}
.svn_form_label{display:block;margin-bottom:5px;font-weight:500;color:#444;font-size:13px;}
.svn_input_text{width:100%;height:34px;line-height:34px;padding:0 10px;border:1px solid #ccc;border-radius:2px;font-size:13px;}
.svn_textarea{width:100%;padding:8px 10px;border:1px solid #ccc;border-radius:2px;font-size:13px;resize:vertical;min-height:80px;}
.svn_select{width:100%;height:34px;padding:0 10px;border:1px solid #ccc;border-radius:2px;font-size:13px;}
/* 按钮样式 */
.svn_btn{display:inline-block;height:32px;line-height:30px;padding:0 15px;border:1px solid #ccc;border-radius:2px;font-size:13px;cursor:pointer;margin-right:10px;background:#fff;}
.svn_btn_success{background:#5cb85c;border-color:#5cb85c;color:#fff;}
.svn_btn_success:hover{background:#4cae4c;border-color:#4cae4c;}
.svn_btn_danger{background:#d9534f;border-color:#d9534f;color:#fff;}
.svn_btn_danger:hover{background:#c9302c;border-color:#c9302c;}
.svn_btn_primary{background:#337ab7;border-color:#337ab7;color:#fff;}
.svn_btn_primary:hover{background:#286090;border-color:#286090;}
/* 站点列表样式 */
.svn_repo_list{margin-top:15px;}
.svn_repo_item{background:#fff;border:1px solid #ececec;border-radius:3px;margin-bottom:15px;padding:15px;}
.svn_repo_header{display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;padding-bottom:10px;border-bottom:1px solid #ececec;}
.svn_repo_title{font-size:16px;font-weight:500;color:#444;}
.svn_repo_info{margin-bottom:8px;}
.svn_user_tags{display:flex;flex-wrap:wrap;gap:8px;margin-top:8px;}
.svn_user_tag{background:#f0f0f1;border:1px solid #ddd;border-radius:3px;padding:4px 8px;font-size:13px;display:inline-flex;align-items:center;}
.svn_user_tag_remove{margin-left:5px;cursor:pointer;color:#d9534f;font-weight:bold;}
/* 辅助文本 */
.svn_help_text{color:#999;font-size:12px;margin-top:5px;}
/* 复制按钮 */
.svn_copy_btn{background:#95a5a6;color:white;border:none;border-radius:3px;padding:2px 6px;margin-left:8px;cursor:pointer;font-size:12px;}
.svn_copy_btn:hover{background:#7f8c8d;}
/* 复选框样式 */
.svn_checkbox_group{display:flex;align-items:center;margin-bottom:15px;}
.svn_checkbox_group input [type= "checkbox " ]{width:auto;margin-right:8px;}
/* 输入组样式 */
.svn_input_group{display:inline-block;vertical-align:middle;width:300px;}
.svn_input_group .svn_input_text{width:255px;margin-right:5px;}
.svn_input_group_btn{display:inline-block;vertical-align:middle;}
/* 表格样式 */
.svn_table{width:100%;border:1px solid #ececec;margin-top:15px;}
.svn_table >thead >tr{background:#f6f6f6;}
.svn_table >tbody >tr >td,.svn_table >thead >tr >th{padding:8px;vertical-align:middle;border-bottom:1px solid #ececec;font-size:13px;}
.svn_table >thead >tr >th{height:25px;font-weight:500;line-height:25px;}
.svn_table >tbody >tr >td{height:20px;color:#444;line-height:20px;}
/* 帮助信息列表样式 */
.svn_help_info_text{color:#777;margin-top:20px;padding-left:25px;}
.svn_help_info_text li{list-style-type:disc;margin-bottom:5px;line-height:1.5;}
/* 系统状态信息样式 */
.svn_system_status_info{margin-top:15px;padding:10px;background:#f8f9fa;border-radius:5px;border:1px solid #e9ecef;}
/* 加载中样式 */
.svn_loading{text-align:center;padding:20px;color:#999;}
/* 关于页面样式 */
.svn_about_content{padding:30px;}
.svn_about_title{color:#20a53a;margin-bottom:20px;}
.svn_about_info{line-height:1.8;}
.svn_about_info p{margin-bottom:10px;}
/* 分页样式 */
.svn_pagination{float:right;padding:0 5px;height:30px;font-size:13px;}
.svn_pagination span{display:inline-block;margin-right:2px;width:30px;height:30px;border-radius:2px;background-color:#5cb85c;color:#fff;text-align:center;line-height:30px;}
.svn_pagination a{display:inline-block;margin:0 3px;padding:0 10px;height:30px;border-radius:2px;background-color:#f5f5f5;color:#666;text-align:center;line-height:30px;cursor:pointer;text-decoration:none;font-size:13px;}
.svn_pagination a:hover{background:#e5e5e5;}
    </style>
</head>
<body>
    <div class="svn_conter">
        <!-- 左侧菜单 -->
        <ul class="svn_left_menu">
            <li class="active" data-type="status" onclick="showTab('status')">
                <span class="icon"><i class="iconfont iconshouye"></i></span>
                <span>系统状态</span>
            </li>
            <li data-type="create" onclick="showTab('create')">
                <span class="icon"><i class="iconfont iconpeizhi"></i></span>
                <span>创建站点</span>
            </li>
            <li data-type="manage" onclick="showTab('manage')">
                <span class="icon"><i class="iconfont iconwangzhan"></i></span>
                <span>站点管理</span>
            </li>
            <li data-type="logs" onclick="showTab('logs')">
                <span class="icon"><i class="iconfont iconcaozuorizhi"></i></span>
                <span>操作日志</span>
            </li>
            <li data-type="about" onclick="showTab('about')">
                <span class="icon"><i class="iconfont iconanquan"></i></span>
                <span>关于</span>
            </li>
        </ul>
        
        <!-- 右侧内容区域 -->
        <div class="svn_rigth_body pd20">
            <!-- 系统状态 -->
            <div class="item_con active" data-type="status">
                <div class="svn_status_grid">
                    <div class="svn_status_card">
                        <div class="svn_status_title">SVN安装状态</div>
                        <div class="svn_status_value" id="svn-installed">检查中...</div>
                    </div>
                    
                    <div class="svn_status_card">
                        <div class="svn_status_title">SVN版本</div>
                        <div class="svn_status_value" id="svn-version">检查中...</div>
                    </div>
                    
                    <div class="svn_status_card">
                        <div class="svn_status_title">SVN服务状态</div>
                        <div class="svn_status_value" id="svn-status">检查中...</div>
                    </div>
                    
                    <div class="svn_status_card">
                        <div class="svn_status_title">SVN根目录</div>
                        <div class="svn_status_value" id="svn-root" style="font-size: 14px;">检查中...</div>
                    </div>
                    
                    <div class="svn_status_card">
                        <div class="svn_status_title">3690端口状态</div>
                        <div class="svn_status_value" id="svn-port-status" style="font-size: 14px;">检查中...</div>
                    </div>
                    
                    <div class="svn_status_card">
                        <div class="svn_status_title">服务器外网IP</div>
                        <div class="svn_status_value" id="server-ip" style="font-size: 14px;">检查中...</div>
                    </div>
                </div>
                
                <div style="margin-top: 15px;">
                    <button class="svn_btn svn_btn_success" onclick="restartSvn()">重启SVN服务</button>
                    <button class="svn_btn svn_btn_danger" onclick="stopSvn()">停止SVN服务</button>
                    <button class="svn_btn svn_btn_primary" onclick="refreshStatus()">刷新状态</button>
                </div>
            </div>
            
            <!-- 创建站点 -->
            <div class="item_con" data-type="create">
                <form id="create-form">
                    <div class="svn_form_group">
                        <label for="project-name" class="svn_form_label">项目名称</label>
                        <input type="text" id="project-name" name="project_name" class="svn_input_text" placeholder="输入项目名称，如: myproject" onblur="generateDefaultUser()">
                        <div class="svn_help_text">项目名称将用作SVN仓库名称，只能包含字母、数字、下划线</div>
                    </div>
                    
                    <div class="svn_form_group">
                        <label for="site-list" class="svn_form_label">选择现有站点</label>
                        <select id="site-list" name="site_list" class="svn_select" onchange="fillWebDir()">
                            <option value="">-- 请选择站点 --</option>
                        </select>
                        <div class="svn_help_text">选择现有站点将自动填充网站目录，也可手动输入</div>
                    </div>
                    
                    <div class="svn_form_group">
                        <label for="web-dir" class="svn_form_label">网站目录</label>
                        <div class="svn_input_group">
                            <input type="text" id="web-dir" name="web_dir" class="svn_input_text" style="width: 255px;" placeholder="输入网站目录路径，如: /www/wwwroot/mysite">
                            <span class="svn_input_group_btn">
                                <span data-id="web-dir" class="glyphicon cursor ml5 glyphicon-folder-open"></span>
                            </span>
                        </div>
                        <div class="svn_help_text">SVN代码将自动同步到此目录</div>
                    </div>
                    
                    <div class="svn_form_group">
                        <label for="users" class="svn_form_label">用户信息</label>
                        <textarea id="users" name="users" class="svn_textarea" rows="3" placeholder="格式: 用户名1:密码1,用户名2:密码2&#10;例如: admin:123456,dev:dev123"></textarea>
                        <div class="svn_help_text">支持多个用户，用逗号分隔，格式为 用户名:密码</div>
                    </div>
                    
                    <div class="svn_checkbox_group">
                        <input type="checkbox" id="auto-generate-user" name="auto_generate_user" checked onchange="toggleUserGeneration()">
                        <label style="margin-top:5px;margin-bottom:0px;" for="auto-generate-user">自动生成默认用户（用户名=项目名，密码随机）</label>
                    </div>
                    
                    <div style="margin-top: 15px;">
                        <button type="button" class="svn_btn svn_btn_primary" onclick="createSite()">创建站点</button>
                        <button type="button" class="svn_btn" onclick="loadSiteList()">刷新站点列表</button>
                    </div>
                </form>
            </div>
            
            <!-- 站点管理 -->
            <div class="item_con" data-type="manage">
                <div style="margin-bottom: 15px;">
                    <button class="svn_btn svn_btn_primary" onclick="loadRepositories()">刷新列表</button>
                </div>
                
                <div id="repo-list" class="svn_repo_list">
                    <div class="svn_loading">加载中...</div>
                </div>
            </div>
            
            <!-- 操作日志 -->
            <div class="item_con" data-type="logs">
                <div style="margin-bottom: 15px;">
                    <button class="svn_btn svn_btn_primary" onclick="loadLogs()">刷新日志</button>
                    <button class="svn_btn svn_btn_danger" onclick="clearLogs()">清空日志</button>
                </div>
                
                <div id="logs-content">
                    <div class="svn_loading">加载中...</div>
                </div>
            </div>
            
            <!-- 关于 -->
            <div class="item_con" data-type="about">
                <div class="svn_about_content">
                    <h2 class="svn_about_title">SVN自动化部署</h2>
                    <div class="svn_about_info">
                        <p><strong>插件名称：</strong>SVN自动化部署</p>
                        <p><strong>插件版本：</strong>1.1.0</p>
                        <p><strong>插件类型：</strong>开发工具</p>
                        <p><strong>作者：</strong>xiaogg</p>
                        <p><strong>发布日期：</strong>2025-11-25</p>
                        <p><strong>功能：</strong>支持一键创建SVN仓库、用户管理、自动同步到网站目录</p>
                    </div>
                </div>
            </div>
            
            <div class="loading_mask"></div>
        </div>
    </div>

    <script type="text/javascript">
        var svn_auto_deploy = {
            plugin_name: 'svn_auto_deploy',
            currentPage: 1,
            pageSize: 10,
            totalRepos: 0,
            
            // 初始化
            init: function() {
                var _this = this;
                setTimeout(function() {
                    $('.layui-layer-page').width(920).css({
                        'top': (document.body.clientHeight - 690) / 2 + 'px',
                    });
                }, 100);
                
                // 初始化页面
                setTimeout(function() {
                    _this.refreshStatus();
                }, 50);
            },
            
            // 显示选项卡
            showTab: function(tabName) {
                $('.svn_left_menu li').removeClass('active');
                $(`.svn_left_menu li[data-type="${tabName}"]`).addClass('active');
                
                $('.item_con').removeClass('active');
                $(`.item_con[data-type="${tabName}"]`).addClass('active');
                
                // 如果切换到日志页面，加载日志
                if (tabName === 'create') {
                    this.loadSiteList();
                }
                if (tabName === 'logs') {
                    this.loadLogs();
                }
                if (tabName === 'manage') {
                    this.loadRepositories();
                }
            },
            
            // 刷新状态
            refreshStatus: function() {
                // 先将所有状态项设置为检查中
                $('#svn-installed').text('检查中...');
                $('#svn-version').text('检查中...');
                $('#svn-status').text('检查中...').removeClass('svn_status_running svn_status_stopped');
                $('#svn-root').text('检查中...');
                $('#svn-port-status').text('检查中...');
                $('#server-ip').text('检查中...');
                
                $.post('/plugin?action=a&name=svn_auto_deploy&s=get_status', {}, function(data) {
                    if (data.status) {
                        $('#svn-installed').text(data.msg.svn_installed ? '已安装' : '未安装');
                        $('#svn-version').text(data.msg.svn_version);
                        if (data.msg.svn_running) {
                            $('#svn-status').text('运行中').addClass('svn_status_running').removeClass('svn_status_stopped');
                        } else {
                            $('#svn-status').text('已停止').addClass('svn_status_stopped').removeClass('svn_status_running');
                        }
                        $('#svn-root').text(data.msg.svn_root);
                        $('#svn-port-status').text(data.msg.port_status);
                        $('#server-ip').text(data.msg.server_ip);
                    }
                }, 'json');
            },
            
            // 加载站点列表
            loadSiteList: function() {
                $.post('/plugin?action=a&name=svn_auto_deploy&s=get_site_list', {}, function(data) {
                    var siteList = $('#site-list');
                    siteList.empty();
                    siteList.append('<option value="">-- 请选择站点 --</option>');
                    
                    if (data.status && data.msg.length > 0) {
                        data.msg.forEach(function(site) {
                            siteList.append('<option value="' + site.path + '">' + site.name + ' (' + site.path + ')</option>');
                        });
                    }
                }, 'json');
            },
            
            // 填充网站目录
            fillWebDir: function() {
                var selectedPath = $('#site-list').val();
                if (selectedPath) {
                    $('#web-dir').val(selectedPath);
                }
            },
            
            // 生成默认用户
            generateDefaultUser: function() {
                if ($('#auto-generate-user').prop('checked')) {
                    var projectName = $('#project-name').val().trim();
                    if (projectName) {
                        // 生成随机密码
                        var randomPassword = Math.random().toString(36).slice(-8);
                        $('#users').val(projectName + ':' + randomPassword);
                    }
                }
            },
            
            // 切换用户生成
            toggleUserGeneration: function() {
                if ($('#auto-generate-user').prop('checked')) {
                    this.generateDefaultUser();
                } else {
                    $('#users').val('');
                }
            },
            
            // 创建站点
            createSite: function() {
                var formData = {
                    project_name: $('#project-name').val(),
                    web_dir: $('#web-dir').val(),
                    users: $('#users').val()
                };

                if (!formData.project_name || !formData.web_dir || !formData.users) {
                    layer.msg('请填写完整信息', {icon: 2});
                    return;
                }

                var loadT = layer.msg('正在创建站点，请稍候...', {icon: 16, time: 0, shade: [0.3, '#000']});
                
                $.post('/plugin?action=a&name=svn_auto_deploy&s=create_site', formData, function(data) {
                    layer.close(loadT);
                    if (data.status) {
                        layer.msg(data.msg, {icon: 1});
                        $('#create-form')[0].reset();
                        setTimeout(function() {
                            svn_auto_deploy.loadRepositories();
                        }, 500);
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                }, 'json');
            },
            
            // 加载仓库列表
            loadRepositories: function() {
                $('#repo-list').html('<div class="svn_loading">加载中...</div>');
                
                $.post('/plugin?action=a&name=svn_auto_deploy&s=get_repositories', {}, function(data) {
                    if (data.status) {
                        var html = '';
                        if (data.msg.length === 0) {
                            html = '<div class="svn_repo_item"><p>暂无SVN站点</p></div>';
                        } else {
                            data.msg.forEach(function(repo) {
                                html += '<div class="svn_repo_item">';
                                html += '<div class="svn_repo_header">';
                                html += '<div class="svn_repo_title">' + repo.name + '</div>';
                                html += '<div>';
                                html += '<button class="svn_btn svn_btn_success" onclick="svn_auto_deploy.showAddUser(\'' + repo.name + '\')">添加用户</button>';
                                html += '<button class="svn_btn svn_btn_danger" onclick="svn_auto_deploy.deleteRepository(\'' + repo.name + '\')">删除仓库</button>';
                                html += '</div>';
                                html += '</div>';
                                
                                html += '<div class="svn_repo_info">';
                                html += '<div style="margin-bottom: 8px;">';
                                html += '<strong>SVN地址:</strong>';
                                html += '<span id="svn-url-' + repo.name + '">' + repo.url + '</span>';
                                html += '<button class="svn_copy_btn" onclick="svn_auto_deploy.copySvnUrl(\'' + repo.name + '\')" title="复制SVN地址">复制</button>';
                                html += '</div>';
                                html += '<div style="margin-bottom: 8px;">';
                                html += '<strong>仓库路径:</strong> ' + repo.path;
                                html += '</div>';
                                html += '<div>';
                                html += '<strong>用户列表:</strong>';
                                html += '<div class="svn_user_tags">';
                                repo.users.forEach(function(user) {
                                    html += '<div class="svn_user_tag">';
                                    html += user;
                                    html += '<span class="svn_user_tag_remove" onclick="svn_auto_deploy.deleteUser(\'' + repo.name + '\', \'' + user + '\')">×</span>';
                                    html += '</div>';
                                });
                                html += '</div>';
                                html += '</div>';
                                html += '</div>';
                                html += '</div>';
                            });
                        }
                        $('#repo-list').html(html);
                    } else {
                        $('#repo-list').html('<div class="svn_repo_item"><p>加载失败: ' + data.msg + '</p></div>');
                    }
                }, 'json');
            },
            
            // 删除仓库
            deleteRepository: function(repoName) {
                layer.open({
                    type: 1,
                    title: '删除仓库 - ' + repoName,
                    area: ['400px', '250px'],
                    content: '<div style="padding:20px;">' +
                        '<p style="margin-bottom:15px;">确定要删除仓库 <strong>' + repoName + '</strong> 吗？此操作不可恢复！</p>' +
                        '<div class="svn_checkbox_group">' +
                        '<input type="checkbox" id="delete_svn_dirs" name="delete_svn_dirs" value="1">' +
                        '<label style="margin-top:5px;margin-bottom:0px;" for="delete_svn_dirs">同时删除关联的.svn目录</label>' +
                        '</div>' +
                        '<p style="color:#999;font-size:12px;margin-top:5px;">勾选此项将同时删除网站目录中与此仓库关联的所有.svn目录</p>' +
                        '</div>',
                    btn: ['确定删除', '取消'],
                    btnAlign: 'c',
                    yes: function(index, layero) {
                        var deleteSvnDirs = $('#delete_svn_dirs').is(':checked') ? 1 : 0;
                        var loadT = layer.msg('正在删除...', {icon: 16, time: 0});
                        $.post('/plugin?action=a&name=svn_auto_deploy&s=remove_repository', {
                            project_name: repoName,
                            delete_svn_dirs: deleteSvnDirs
                        }, function(data) {
                            layer.close(index);
                            layer.msg(data.msg, {icon: data.status ? 1 : 2});
                            if (data.status) {
                                svn_auto_deploy.loadRepositories();
                            }
                        }, 'json');
                    }
                });
            },
            
            // 显示添加用户对话框
            showAddUser: function(repoName) {
                var formHtml = '<div style="padding: 20px;">'+
                    '<div class="svn_form_group">'+
                        '<label class="svn_form_label">用户名</label>'+
                        '<input type="text" id="add-username" class="svn_input_text" placeholder="输入用户名">'+
                    '</div>'+
                    '<div class="svn_form_group">'+
                        '<label class="svn_form_label">密码</label>'+
                        '<input type="password" id="add-password" class="svn_input_text" placeholder="输入密码">'+
                    '</div>'+
                    '<div class="svn_form_group">'+
                        '<label class="svn_form_label">权限</label>'+
                        '<select id="add-permission" class="svn_select">'+
                            '<option value="rw" selected>读写 (rw)</option>'+
                            '<option value="r">只读 (r)</option>'+
                            '<option value="w">只写 (w)</option>'+
                            '<option value="">无权限</option>'+
                        '</select>'+
                    '</div>'+
                    '</div>';
                    
                layer.open({
                    type: 1,
                    title: '添加用户到 ' + repoName,
                    area: ['400px', '300px'],
                    content: formHtml,
                    btn: ['确定', '取消'],
                    yes: function(index, layero) {
                        var username = $('#add-username').val().trim();
                        var password = $('#add-password').val().trim();
                        var permission = $('#add-permission').val();
                        
                        if (!username) {
                            layer.msg('请输入用户名', {icon: 2});
                            return;
                        }
                        
                        if (!password) {
                            layer.msg('请输入密码', {icon: 2});
                            return;
                        }
                        
                        layer.close(index);
                        
                        var loadT = layer.msg('正在添加用户...', {icon: 16, time: 0});
                        
                        $.post('/plugin?action=a&name=svn_auto_deploy&s=add_repository_user', {
                            project_name: repoName,
                            username: username,
                            password: password,
                            permission: permission
                        }, function(data) {
                            layer.close(loadT);
                            layer.msg(data.msg, {icon: data.status ? 1 : 2});
                            if (data.status) {
                                svn_auto_deploy.loadRepositories();
                            }
                        }, 'json');
                    }
                });
            },
            
            // 删除用户
            deleteUser: function(repoName, username) {
                layer.confirm('确定要删除用户 ' + username + ' 吗？', {
                    btn: ['确定', '取消']
                }, function() {
                    $.post('/plugin?action=a&name=svn_auto_deploy&s=remove_repository_user', {
                        project_name: repoName,
                        username: username
                    }, function(data) {
                        layer.msg(data.msg, {icon: data.status ? 1 : 2});
                        if (data.status) {
                            svn_auto_deploy.loadRepositories();
                        }
                    }, 'json');
                });
            },
            
            // 重启SVN服务
            restartSvn: function() {
                var loadT = layer.msg('正在重启SVN服务...', {icon: 16, time: 0});
                $.post('/plugin?action=a&name=svn_auto_deploy&s=restart_svn', {}, function(data) {
                    layer.close(loadT);
                    layer.msg(data.msg, {icon: data.status ? 1 : 2});
                    svn_auto_deploy.refreshStatus();
                }, 'json');
            },
            
            // 停止SVN服务
            stopSvn: function() {
                layer.confirm('确定要停止SVN服务吗？', {
                    btn: ['确定', '取消']
                }, function() {
                    var loadT = layer.msg('正在停止SVN服务...', {icon: 16, time: 0});
                    $.post('/plugin?action=a&name=svn_auto_deploy&s=stop_svn', {}, function(data) {
                        layer.close(loadT);
                        layer.msg(data.msg, {icon: data.status ? 1 : 2});
                        svn_auto_deploy.refreshStatus();
                    }, 'json');
                });
            },
            
            // 复制SVN地址
            copySvnUrl: function(repoName) {
                var svnUrlElement = document.getElementById('svn-url-' + repoName);
                var svnUrl = svnUrlElement.textContent;
                
                if (navigator.clipboard && window.isSecureContext) {
                    navigator.clipboard.writeText(svnUrl).then(function() {
                        layer.msg('SVN地址复制成功', {icon: 1, time: 2000});
                    }).catch(function(err) {
                        svn_auto_deploy.fallbackCopyTextToClipboard(svnUrl);
                    });
                } else {
                    svn_auto_deploy.fallbackCopyTextToClipboard(svnUrl);
                }
            },
            
            // 复制到剪贴板的备用方法
            fallbackCopyTextToClipboard: function(text) {
                var textArea = document.createElement("textarea");
                textArea.value = text;
                
                textArea.style.top = "0";
                textArea.style.left = "0";
                textArea.style.position = "fixed";

                document.body.appendChild(textArea);
                textArea.focus();
                textArea.select();

                try {
                    var successful = document.execCommand('copy');
                    var msg = successful ? 'SVN地址复制成功': 'SVN地址复制失败';
                    layer.msg(msg, {icon: successful ? 1 : 2, time: 2000});
                } catch (err) {
                    layer.msg('复制失败: ' + text, {icon: 2, time: 3000});
                }
                document.body.removeChild(textArea);
            },
            
            // 加载操作日志
            loadLogs: function(page) {
                page = page || 1;
                $('#logs-content').html('<div class="svn_loading">加载中...</div>');
                
                $.post('/plugin?action=a&name=svn_auto_deploy&s=get_operation_logs', {
                    page: page,
                    page_size: 20
                }, function(data) {
                    if (data.status) {
                        var html = '';
                        
                        // 添加日志表格
                        html += '<table class="svn_table">';
                        html += '<thead>';
                        html += '<tr>';
                        html += '<th>时间</th>';
                        html += '<th>操作</th>';
                        html += '<th>状态</th>';
                        html += '<th>详情</th>';
                        html += '<th>用户</th>';
                        html += '</tr>';
                        html += '</thead>';
                        html += '<tbody>';
                        
                        if (data.msg.logs.length === 0) {
                            html += '<tr><td colspan="5" align="center">暂无日志记录</td></tr>';
                        } else {
                            data.msg.logs.forEach(function(log) {
                                html += '<tr>';
                                html += '<td>' + log.time + '</td>';
                                html += '<td>' + log.operation + '</td>';
                                html += '<td>' + (log.status === '成功' ? 
                                    '<span style="color: #5cb85c;">' + log.status + '</span>' : 
                                    '<span style="color: #d9534f;">' + log.status + '</span>') + '</td>';
                                html += '<td>' + (log.details || '') + '</td>';
                                html += '<td>' + log.user + '</td>';
                                html += '</tr>';
                            });
                        }
                        
                        html += '</tbody>';
                        html += '</table>';
                        
                        // 添加分页
                        if (data.msg.total_pages > 1) {
                            html += '<div class="svn_pagination" style="margin-top: 15px; text-align: center;">';
                            
                            // 上一页
                            if (data.msg.page > 1) {
                                html += '<a href="javascript:void(0);" onclick="svn_auto_deploy.loadLogs(' + (data.msg.page - 1) + ')">上一页</a>';
                            }
                            
                            // 优化页码显示 - 只显示当前页附近的页码
                            var startPage = Math.max(1, data.msg.page - 5);
                            var endPage = Math.min(data.msg.total_pages, data.msg.page + 5);
                            
                            // 如果不是从第一页开始，显示第一页和省略号
                            if (startPage > 1) {
                                html += '<a href="javascript:void(0);" onclick="svn_auto_deploy.loadLogs(1)">1</a>';
                                if (startPage > 2) {
                                    html += '<span style="padding: 0 5px;">...</span>';
                                }
                            }
                            
                            // 显示当前页附近的页码
                            for (var i = startPage; i <= endPage; i++) {
                                if (i === data.msg.page) {
                                    html += '<span class="active">' + i + '</span>';
                                } else {
                                    html += '<a href="javascript:void(0);" onclick="svn_auto_deploy.loadLogs(' + i + ')">' + i + '</a>';
                                }
                            }
                            
                            // 如果不是到最后一页结束，显示省略号和最后一页
                            if (endPage < data.msg.total_pages) {
                                if (endPage < data.msg.total_pages - 1) {
                                    html += '<span style="padding: 0 5px;">...</span>';
                                }
                                html += '<a href="javascript:void(0);" onclick="svn_auto_deploy.loadLogs(' + data.msg.total_pages + ')">' + data.msg.total_pages + '</a>';
                            }
                            
                            // 下一页
                            if (data.msg.page < data.msg.total_pages) {
                                html += '<a href="javascript:void(0);" onclick="svn_auto_deploy.loadLogs(' + (data.msg.page + 1) + ')">下一页</a>';
                            }
                            
                            html += '<span style="margin-left: 15px;">共 ' + data.msg.total + ' 条记录，第 ' + data.msg.page + ' / ' + data.msg.total_pages + ' 页</span>';
                            html += '</div>';
                        }
                        
                        $('#logs-content').html(html);
                    } else {
                        $('#logs-content').html('<div class="svn_repo_item"><p>加载失败: ' + data.msg + '</p></div>');
                    }
                }, 'json');
            },
            
            // 清空操作日志
            clearLogs: function() {
                layer.confirm('确定要清空所有操作日志吗？此操作不可恢复！', {
                    btn: ['确定清空', '取消']
                }, function() {
                    var loadT = layer.msg('正在清空日志...', {icon: 16, time: 0});
                    $.post('/plugin?action=a&name=svn_auto_deploy&s=clear_operation_logs', {}, function(data) {
                        layer.close(loadT);
                        layer.msg(data.msg, {icon: data.status ? 1 : 2});
                        if (data.status) {
                            // 清空成功后重新加载日志
                            setTimeout(function() {
                                svn_auto_deploy.loadLogs(1);
                            }, 500);
                        }
                    }, 'json');
                });
            }
        };
        
        // 兼容原有的全局函数
        function showTab(tabName) {
            svn_auto_deploy.showTab(tabName);
        }
        
        function refreshStatus() {
            svn_auto_deploy.refreshStatus();
        }
        
        function loadSiteList() {
            svn_auto_deploy.loadSiteList();
        }
        
        function fillWebDir() {
            svn_auto_deploy.fillWebDir();
        }
        
        function generateDefaultUser() {
            svn_auto_deploy.generateDefaultUser();
        }
        
        function toggleUserGeneration() {
            svn_auto_deploy.toggleUserGeneration();
        }
        
        function createSite() {
            svn_auto_deploy.createSite();
        }
        
        function loadRepositories() {
            svn_auto_deploy.loadRepositories();
        }
        
        function deleteRepository(repoName) {
            svn_auto_deploy.deleteRepository(repoName);
        }
        
        function showAddUser(repoName) {
            svn_auto_deploy.showAddUser(repoName);
        }
        
        function deleteUser(repoName, username) {
            svn_auto_deploy.deleteUser(repoName, username);
        }
        
        function restartSvn() {
            svn_auto_deploy.restartSvn();
        }
        
        function stopSvn() {
            svn_auto_deploy.stopSvn();
        }
        
        function copySvnUrl(repoName) {
            svn_auto_deploy.copySvnUrl(repoName);
        }
        
        function loadLogs(page) {
            svn_auto_deploy.loadLogs(page);
        }
        
        function clearLogs() {
            svn_auto_deploy.clearLogs();
        }
        
        // 页面加载时初始化
        $(document).ready(function() {
            svn_auto_deploy.init();
        });
    </script>
</body>
</html>